use crate::handlers::get_user;
use crate::keyboards::standart_keyboard;

use anyhow::Result;

use teloxide::prelude2::*;
use uuid::Uuid;

pub async fn unarchive(bot: AutoSend<Bot>, q: CallbackQuery, data: &str) -> Result<()> {
    println!("test unarchive");
    let user_id = q.from.id;
    let user = get_user(user_id).await?;
    unarchive_post(data, user.id).await?;
    let message_id = q.message.clone().unwrap().id;
    let chat_id = q.message.clone().unwrap().chat_id();
    let text = clear_label(q.message.clone().unwrap().text().unwrap().to_string());
    println!("text = {:#?}", text);
    bot.answer_callback_query(q.id).text("Unarchived!").await?;
    bot.edit_message_text(chat_id, message_id, text).await?;
    bot.edit_message_reply_markup(chat_id, message_id)
        .reply_markup(standart_keyboard(data))
        .await?;
    Ok(())
}

fn clear_label(text: String) -> String {
    if text.contains("[Archived]") {
        text.strip_suffix("[Archived]").unwrap().to_string()
    } else {
        text
    }
}
use crate::db::connect_to_db;
use crate::helpers::uuid_from_str;
use crate::user::UserPost;

use mongodb::bson::doc;
async fn unarchive_post(post_id: &str, user_id: Uuid) -> Result<()> {
    println!("{:#?}", post_id);
    let bin_id = uuid_from_str(post_id)?;
    let db = connect_to_db().await?;
    let coll = db.collection::<UserPost>("userposts");
    //println!("{:#?}", bin_id.to_hyphenated().to_string());
    println!("{:#?}", user_id);
    let query = doc! {
        "user_id": user_id,
        "post_id": &bin_id
    };
    let update = doc! {
        "$set": {
            "read": false
        }
    };
    /*let array_filters = vec![doc! {
        "elem.post_id": bin_id,
    }];
    let options = UpdateOptions::builder()
        .array_filters(array_filters)
        .build();*/
    coll.update_one(query, update, None).await?;
    Ok(())
}
